package com.mirabelle.juice.controller;

import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.faces.event.AjaxBehaviorEvent;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

@Controller("C_SWEET")
@Scope("session")
public class CEcoSweetSpot implements Serializable {
	private static final long	serialVersionUID	= 1L;
	private transient Logger	log					= LoggerFactory.getLogger(getClass());

	private double valueU = 4.0;
	private double valueO = 2.0;
	private double valueW = 8.0;
	private double valueA = 2.0;
	private double boxCost = 7.0;
	private double liquid10Cost = 5.5;
	private int countCig = 20;
	private int totalCig = 0;
	private int totaleCig = 0;
	private int ecoCount = 0;
	
	private boolean modeW = false;
	private boolean modeU = true;
	
	@PostConstruct
	public void init() {
		log.debug("Initialisation du controlleur C_SWEET.");
		computeEco();
	}
	
	public void changeU(AjaxBehaviorEvent e) {
		log.debug("ChangeU appelé avec la valeur : " + valueU);
		valueW = (java.lang.Math.pow(valueU,2.0))/valueO; /* P=U2/R */
		valueA = valueU / valueO; /* i=U/R */
	}
	
	public void changeO(AjaxBehaviorEvent e) {
		log.debug("ChangeO appelé avec la valeur : " + valueO);
		if (true == modeW) {
			valueW = (java.lang.Math.pow(valueU,2.0))/valueO; /* P=U2/R */
		} else {
			valueU = java.lang.Math.sqrt(valueW * valueO); /* U=sqrt(P*R) */
		}
		valueA = valueU / valueO; /* i=U/R */
	}
	
	public void changeW(AjaxBehaviorEvent e) {
		log.debug("ChangeW appelé avec la valeur : " + valueW);
		valueU = java.lang.Math.sqrt(valueW * valueO); /* U=sqrt(P*R) */
		valueA = valueU / valueO; /* i=U/R */
	}
	
	public void changeCountCig(AjaxBehaviorEvent e) {
		log.debug("changeCountCig appelé avec la valeur : " + countCig);
		computeEco();
	}
	
	public void changeBoxCost(AjaxBehaviorEvent e) {
		log.debug("changeBoxCost appelé avec la valeur : " + boxCost);
		computeEco();
	}
	
	public void changeLiquid10Cost(AjaxBehaviorEvent e) {
		log.debug("changeBoxCost appelé avec la valeur : " + boxCost);
		computeEco();
	}
	
	public void changeMode(AjaxBehaviorEvent e) {
		log.debug("changeMode appelé");
		if (true == modeW) {
			modeW = false;
			modeU = true;
			log.debug("mode : calcul de tension" );
		} else {
			modeW = true;
			modeU = false;
			log.debug("mode : calcul de puissance" );
		}
		log.debug("modeU : " + modeU);
		log.debug("modeW : " + modeW);
	}
	
	private void computeEco() {
		totalCig = (int) java.lang.Math.round(((countCig * 365)/20)*boxCost);
		totaleCig = (int) java.lang.Math.round(((countCig * 365)/20)*(liquid10Cost/5)+120);
		ecoCount = totalCig - totaleCig;
	}

	public double getValueU() {
		return valueU;
	}

	public void setValueU(double valueU) {
		this.valueU = valueU;
	}

	public double getValueO() {
		return valueO;
	}

	public void setValueO(double valueO) {
		this.valueO = valueO;
	}

	public double getValueW() {
		return valueW;
	}

	public void setValueW(double valueW) {
		this.valueW = valueW;
	}

	public double getValueA() {
		return valueA;
	}

	public double getBoxCost() {
		return boxCost;
	}

	public void setBoxCost(double boxCost) {
		this.boxCost = boxCost;
	}
	
	public double getLiquid10Cost() {
		return liquid10Cost;
	}

	public void setLiquid10Cost(double liquid10Cost) {
		this.liquid10Cost = liquid10Cost;
	}

	public int getCountCig() {
		return countCig;
	}

	public void setCountCig(int countCig) {
		this.countCig = countCig;
	}

	public int getEcoCount() {
		return ecoCount;
	}
	
	public int getTotalCig() {
		return totalCig;
	}
	
	public int getTotaleCig() {
		return totaleCig;
	}
	
	public int getEcoCountMonth() {
		return ecoCount / 12;
	}
	
	public int getTotalCigMonth() {
		return totalCig / 12;
	}
	
	public int getTotaleCigMonth() {
		return totaleCig / 12;
	}
	
	public boolean isModeW() {
		return modeW;
	}
	
	public boolean isModeU() {
		return modeU;
	}

	public void setModeW(boolean modeW) {
		this.modeW = modeW;
	}
	
	public void setModeU(boolean modeU) {
		this.modeU = modeU;
	}

	public double getValueADC() {
		return valueA / 2;
	}
}
